cmake_minimum_required(VERSION 3.10)


#CPU架构
SET(ARCH "x86" CACHE STRING "select build cpu type")
#使用的交叉编译工具链设置
if("${ARCH}" MATCHES "aarch32")
    set(TOOLCHAIN "/mnt/f/toolchains/gcc-linaro-5.5.0-2017.10-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-")
elseif("${ARCH}" MATCHES "aarch64")
    set(TOOLCHAIN "/mnt/f/toolchains/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-")
elseif("${ARCH}" MATCHES "arm")
    set(TOOLCHAIN "/mnt/f/toolchains/gcc-linaro-5.5.0-2017.10-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-")
elseif("${ARCH}" MATCHES "arm64")
    set(TOOLCHAIN "/mnt/f/toolchains/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-")
elseif("${ARCH}" MATCHES "x86")
    set(TOOLCHAIN "")
elseif("${ARCH}" MATCHES "x86_64")
    set(TOOLCHAIN "")
else()
    set(TOOLCHAIN "")
endif()
#
# set(CMAKE_C_COMPILER  "${TOOLCHAIN}gcc" CACHE STRING "select ${ARCH} build toolchains for C")
# set(CMAKE_CXX_COMPILER  "${TOOLCHAIN}g++" CACHE STRING "select ${ARCH} build toolchains for CXX")
# set(CMAKE_COMPILER_STRIP "${TOOLCHAIN}strip" CACHE STRING "select ${ARCH} build toolchains for strip")
# set(CMAKE_COMPILER_AR "${TOOLCHAIN}ar" CACHE STRING "select ${ARCH} build toolchains for ar")
set(CMAKE_C_COMPILER  "${TOOLCHAIN}gcc")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -D__USE_XOPEN -std=gnu11 -Os -g")
#for strptime function
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE -D__USE_XOPEN")
set(CMAKE_CXX_COMPILER  "${TOOLCHAIN}g++")
set(CMAKE_COMPILER_STRIP "${TOOLCHAIN}strip")
set(CMAKE_COMPILER_AR "${TOOLCHAIN}ar")
#编译及安装路径设置
set(CMAKE_INSTALL_PREFIX "${CMAKE_HOME_DIRECTORY}/install")
set(CMAKE_BUILD_PREFIX "${CMAKE_HOME_DIRECTORY}/target")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BUILD_PREFIX}/${ARCH}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BUILD_PREFIX}/${ARCH}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BUILD_PREFIX}/${ARCH}/bin)
set(CMAKE_INCLUDE_OUTPUT_DIRECTORY ${CMAKE_BUILD_PREFIX}/include)
set(CMAKE_INSTALL_BINARY_DIR "${CMAKE_INSTALL_PREFIX}/${ARCH}/bin")
set(CMAKE_INSTALL_LIBRARY_DIR "${CMAKE_INSTALL_PREFIX}/${ARCH}/lib")
set(CMAKE_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/${ARCH}/include")


project(ped VERSION 1.0.0)

#版本文件
configure_file(config.h.in ${CMAKE_INCLUDE_OUTPUT_DIRECTORY}/version.h)


#头文件及库文件路径设置
include_directories(${CMAKE_HOME_DIRECTORY}/include)
link_directories(${CMAKE_HOME_DIRECTORY}/lib/${ARCH})


#auto generate library path
include_directories(${CMAKE_INCLUDE_OUTPUT_DIRECTORY})
link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
#logger path

execute_process(COMMAND ln "-sf" "libz.so" "libz.so.1"
                WORKING_DIRECTORY ${CMAKE_HOME_DIRECTORY}/lib/aarch32
                TIMEOUT 5)

#source code
# add_subdirectory(./src)
add_subdirectory(./ThirdParty)
#
add_subdirectory(./src)

#打docker包命令
add_custom_target(
    docker
    COMMAND "./docker.sh" "${ARCH}" "SV${PROJECT_VERSION}"
    WORKING_DIRECTORY "${CMAKE_HOME_DIRECTORY}/Package"
)
#打普通包命令
add_custom_target(
    package
    COMMAND "./cpkg32.sh" "${ARCH}" "SV${PROJECT_VERSION}"
    WORKING_DIRECTORY "${CMAKE_HOME_DIRECTORY}/Package"
)


#
# message(status "编译完成")

